//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "ConstantOptionletVolatility.h"
using namespace Cephei::QL::Termstructures::Volatility::Optionlet;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/Volatility/SmileSection.h>
#include <gen/QL/Termstructures/Volatility/Optionlet/OptionletVolatilityStructure.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures::Volatility;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::Optionlet::CConstantOptionletVolatility::CConstantOptionletVolatility (DateTime referenceDate, Cephei::QL::Times::ICalendar^ cal, QL::Times::BusinessDayConventionEnum bdc, Double volatility, Cephei::QL::Times::IDayCounter^ dc) : COptionletVolatilityStructure(CConstantOptionletVolatility::typeid)
{
    CCalendar^ _Ccal;
    CDayCounter^ _Cdc;
    try
    {
#ifdef HANDLE
        _phConstantOptionletVolatility = NULL;
#endif
        QuantLib::Date _referenceDate = (QuantLib::Date)ValueHelper::Convert (referenceDate); //d
        _Ccal = safe_cast<CCalendar^> (cal);
        _Ccal->Lock();
        QuantLib::Calendar& _cal = static_cast<QuantLib::Calendar&> (_Ccal->GetReference ()); 
        QuantLib::BusinessDayConvention _bdc = (QuantLib::BusinessDayConvention)bdc ;
        QuantLib::Volatility _volatility = (QuantLib::Volatility)ValueHelper::Convert (volatility); //d
        _Cdc = safe_cast<CDayCounter^> (dc);
        _Cdc->Lock();
        QuantLib::DayCounter& _dc = static_cast<QuantLib::DayCounter&> (_Cdc->GetReference ()); 
        _ppConstantOptionletVolatility = new boost::shared_ptr<QuantLib::ConstantOptionletVolatility> (new QuantLib::ConstantOptionletVolatility ( _referenceDate,  _cal,  _bdc,  _volatility,  _dc ));
        SetOptionletVolatilityStructure (boost::dynamic_pointer_cast<QuantLib::OptionletVolatilityStructure> (*_ppConstantOptionletVolatility));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccal != nullptr) _Ccal->Unlock();
        if (_Cdc != nullptr) _Cdc->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::Optionlet::CConstantOptionletVolatility::CConstantOptionletVolatility (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ cal, QL::Times::BusinessDayConventionEnum bdc, Double volatility, Cephei::QL::Times::IDayCounter^ dc) : COptionletVolatilityStructure(CConstantOptionletVolatility::typeid)
{
    CCalendar^ _Ccal;
    CDayCounter^ _Cdc;
    try
    {
#ifdef HANDLE
        _phConstantOptionletVolatility = NULL;
#endif
        QuantLib::Natural _settlementDays = (QuantLib::Natural)ValueHelper::Convert (settlementDays); //d
        _Ccal = safe_cast<CCalendar^> (cal);
        _Ccal->Lock();
        QuantLib::Calendar& _cal = static_cast<QuantLib::Calendar&> (_Ccal->GetReference ()); 
        QuantLib::BusinessDayConvention _bdc = (QuantLib::BusinessDayConvention)bdc ;
        QuantLib::Volatility _volatility = (QuantLib::Volatility)ValueHelper::Convert (volatility); //d
        _Cdc = safe_cast<CDayCounter^> (dc);
        _Cdc->Lock();
        QuantLib::DayCounter& _dc = static_cast<QuantLib::DayCounter&> (_Cdc->GetReference ()); 
        _ppConstantOptionletVolatility = new boost::shared_ptr<QuantLib::ConstantOptionletVolatility> (new QuantLib::ConstantOptionletVolatility ( _settlementDays,  _cal,  _bdc,  _volatility,  _dc ));
        SetOptionletVolatilityStructure (boost::dynamic_pointer_cast<QuantLib::OptionletVolatilityStructure> (*_ppConstantOptionletVolatility));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccal != nullptr) _Ccal->Unlock();
        if (_Cdc != nullptr) _Cdc->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::Optionlet::CConstantOptionletVolatility::CConstantOptionletVolatility (DateTime referenceDate, Cephei::QL::Times::ICalendar^ cal, QL::Times::BusinessDayConventionEnum bdc, Cephei::QL::IQuote^ volatility, Cephei::QL::Times::IDayCounter^ dc) : COptionletVolatilityStructure(CConstantOptionletVolatility::typeid)
{
    CCalendar^ _Ccal;
    CQuote^ _Cvolatility;
    CDayCounter^ _Cdc;
    try
    {
#ifdef HANDLE
        _phConstantOptionletVolatility = NULL;
#endif
        QuantLib::Date _referenceDate = (QuantLib::Date)ValueHelper::Convert (referenceDate); //d
        _Ccal = safe_cast<CCalendar^> (cal);
        _Ccal->Lock();
        QuantLib::Calendar& _cal = static_cast<QuantLib::Calendar&> (_Ccal->GetReference ()); 
        QuantLib::BusinessDayConvention _bdc = (QuantLib::BusinessDayConvention)bdc ;
        _Cvolatility = safe_cast<CQuote^> (volatility);
        _Cvolatility->Lock();
        Handle<QuantLib::Quote>& _volatility = static_cast<Handle<QuantLib::Quote>&> (_Cvolatility->GetHandle ()); 
        _Cdc = safe_cast<CDayCounter^> (dc);
        _Cdc->Lock();
        QuantLib::DayCounter& _dc = static_cast<QuantLib::DayCounter&> (_Cdc->GetReference ()); 
        _ppConstantOptionletVolatility = new boost::shared_ptr<QuantLib::ConstantOptionletVolatility> (new QuantLib::ConstantOptionletVolatility ( _referenceDate,  _cal,  _bdc,  _volatility,  _dc ));
        SetOptionletVolatilityStructure (boost::dynamic_pointer_cast<QuantLib::OptionletVolatilityStructure> (*_ppConstantOptionletVolatility));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccal != nullptr) _Ccal->Unlock();
        if (_Cvolatility != nullptr) _Cvolatility->Unlock();
        if (_Cdc != nullptr) _Cdc->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::Optionlet::CConstantOptionletVolatility::CConstantOptionletVolatility (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ cal, QL::Times::BusinessDayConventionEnum bdc, Cephei::QL::IQuote^ volatility, Cephei::QL::Times::IDayCounter^ dc) : COptionletVolatilityStructure(CConstantOptionletVolatility::typeid)
{
    CCalendar^ _Ccal;
    CQuote^ _Cvolatility;
    CDayCounter^ _Cdc;
    try
    {
#ifdef HANDLE
        _phConstantOptionletVolatility = NULL;
#endif
        QuantLib::Natural _settlementDays = (QuantLib::Natural)ValueHelper::Convert (settlementDays); //d
        _Ccal = safe_cast<CCalendar^> (cal);
        _Ccal->Lock();
        QuantLib::Calendar& _cal = static_cast<QuantLib::Calendar&> (_Ccal->GetReference ()); 
        QuantLib::BusinessDayConvention _bdc = (QuantLib::BusinessDayConvention)bdc ;
        _Cvolatility = safe_cast<CQuote^> (volatility);
        _Cvolatility->Lock();
        Handle<QuantLib::Quote>& _volatility = static_cast<Handle<QuantLib::Quote>&> (_Cvolatility->GetHandle ()); 
        _Cdc = safe_cast<CDayCounter^> (dc);
        _Cdc->Lock();
        QuantLib::DayCounter& _dc = static_cast<QuantLib::DayCounter&> (_Cdc->GetReference ()); 
        _ppConstantOptionletVolatility = new boost::shared_ptr<QuantLib::ConstantOptionletVolatility> (new QuantLib::ConstantOptionletVolatility ( _settlementDays,  _cal,  _bdc,  _volatility,  _dc ));
        SetOptionletVolatilityStructure (boost::dynamic_pointer_cast<QuantLib::OptionletVolatilityStructure> (*_ppConstantOptionletVolatility));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Ccal != nullptr) _Ccal->Unlock();
        if (_Cvolatility != nullptr) _Cvolatility->Unlock();
        if (_Cdc != nullptr) _Cdc->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::Optionlet::CConstantOptionletVolatility::CConstantOptionletVolatility (boost::shared_ptr<QuantLib::ConstantOptionletVolatility>& childNative, Object^ owner) : COptionletVolatilityStructure(CConstantOptionletVolatility::typeid)
{
#ifdef HANDLE
	_phConstantOptionletVolatility = NULL;
#endif
	_ppConstantOptionletVolatility = &childNative;
    _ppOptionletVolatilityStructure = new boost::shared_ptr<QuantLib::OptionletVolatilityStructure> (boost::dynamic_pointer_cast<QuantLib::OptionletVolatilityStructure> (*_ppConstantOptionletVolatility));
}
Cephei::QL::Termstructures::Volatility::Optionlet::CConstantOptionletVolatility::CConstantOptionletVolatility (QuantLib::ConstantOptionletVolatility& childNative, Object^ owner) : COptionletVolatilityStructure(CConstantOptionletVolatility::typeid)
{
#ifdef HANDLE
	_phConstantOptionletVolatility = NULL;
#endif
	_ppConstantOptionletVolatility = new boost::shared_ptr<QuantLib::ConstantOptionletVolatility> (&childNative);
    _ppOptionletVolatilityStructure = new boost::shared_ptr<QuantLib::OptionletVolatilityStructure> (boost::dynamic_pointer_cast<QuantLib::OptionletVolatilityStructure> (*_ppConstantOptionletVolatility));
    _ConstantOptionletVolatilityOwner = owner;
    _OptionletVolatilityStructureOwner = owner;
}

Cephei::QL::Termstructures::Volatility::Optionlet::CConstantOptionletVolatility::CConstantOptionletVolatility (CConstantOptionletVolatility^ copy) : COptionletVolatilityStructure(CConstantOptionletVolatility::typeid)
{
#ifdef HANDLE
	_phConstantOptionletVolatility = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppConstantOptionletVolatility = new boost::shared_ptr<QuantLib::ConstantOptionletVolatility> (copy->GetShared());
        _ppOptionletVolatilityStructure = new boost::shared_ptr<QuantLib::OptionletVolatilityStructure> (boost::dynamic_pointer_cast<QuantLib::OptionletVolatilityStructure> (*_ppConstantOptionletVolatility));
    }
}
Cephei::QL::Termstructures::Volatility::Optionlet::CConstantOptionletVolatility::CConstantOptionletVolatility (PLATFORM::Type^ t) : COptionletVolatilityStructure(CConstantOptionletVolatility::typeid)
{
#ifdef HANDLE
	_phConstantOptionletVolatility = NULL;
#endif
	if (!t->IsSubclassOf(CConstantOptionletVolatility::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::Optionlet::CConstantOptionletVolatility::CConstantOptionletVolatility (QuantLib::Handle<QuantLib::ConstantOptionletVolatility>& childNative, Object^ owner)  : COptionletVolatilityStructure(CConstantOptionletVolatility::typeid)
{
	_phConstantOptionletVolatility = &childNative;
	_ppConstantOptionletVolatility = &static_cast<boost::shared_ptr<QuantLib::ConstantOptionletVolatility>>(childNative.currentLink());
    _ppOptionletVolatilityStructure = new boost::shared_ptr<QuantLib::OptionletVolatilityStructure> (boost::dynamic_pointer_cast<QuantLib::OptionletVolatilityStructure> (*_ppConstantOptionletVolatility));
    _ConstantOptionletVolatilityOwner = owner;
}
Cephei::QL::Termstructures::Volatility::Optionlet::CConstantOptionletVolatility::CConstantOptionletVolatility (QuantLib::Handle<QuantLib::ConstantOptionletVolatility> childNative)  : COptionletVolatilityStructure(CConstantOptionletVolatility::typeid)
{
	_phConstantOptionletVolatility = &childNative;
	_ppConstantOptionletVolatility = &static_cast<boost::shared_ptr<QuantLib::ConstantOptionletVolatility>>(childNative.currentLink());
    _ppOptionletVolatilityStructure = new boost::shared_ptr<QuantLib::OptionletVolatilityStructure> (boost::dynamic_pointer_cast<QuantLib::OptionletVolatilityStructure> (*_ppConstantOptionletVolatility));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::Optionlet::CConstantOptionletVolatility::CConstantOptionletVolatility (QuantLib::ConstantOptionletVolatility childNative)  : COptionletVolatilityStructure(CConstantOptionletVolatility::typeid)
{
#ifdef HANDLE
	_phConstantOptionletVolatility = NULL;
#endif
	_ppConstantOptionletVolatility = new boost::shared_ptr<QuantLib::ConstantOptionletVolatility> (new QuantLib::ConstantOptionletVolatility (childNative));
    _ppOptionletVolatilityStructure = new boost::shared_ptr<QuantLib::OptionletVolatilityStructure> (boost::dynamic_pointer_cast<QuantLib::OptionletVolatilityStructure> (*_ppConstantOptionletVolatility));
}
#endif

Cephei::QL::Termstructures::Volatility::Optionlet::CConstantOptionletVolatility::~CConstantOptionletVolatility ()
{
    if (_ppConstantOptionletVolatility != NULL)
    {
	    delete _ppConstantOptionletVolatility;
        _ppConstantOptionletVolatility = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::Optionlet::CConstantOptionletVolatility::!CConstantOptionletVolatility ()
{
    if (_ppConstantOptionletVolatility != NULL)
    {
	    delete _ppConstantOptionletVolatility;
    }
}
QuantLib::ConstantOptionletVolatility& Cephei::QL::Termstructures::Volatility::Optionlet::CConstantOptionletVolatility::GetReference ()
{
    if (_ppConstantOptionletVolatility == NULL) throw REFNEW NativeNullException ();
	return **_ppConstantOptionletVolatility;
}
boost::shared_ptr<QuantLib::ConstantOptionletVolatility>& Cephei::QL::Termstructures::Volatility::Optionlet::CConstantOptionletVolatility::GetShared ()
{
    if (_ppConstantOptionletVolatility == NULL) throw REFNEW NativeNullException ();
	return *_ppConstantOptionletVolatility;
}
QuantLib::ConstantOptionletVolatility* Cephei::QL::Termstructures::Volatility::Optionlet::CConstantOptionletVolatility::GetPointer ()
{
    if (_ppConstantOptionletVolatility == NULL) throw REFNEW NativeNullException ();
	return &**_ppConstantOptionletVolatility;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::ConstantOptionletVolatility>& Cephei::QL::Termstructures::Volatility::Optionlet::CConstantOptionletVolatility::GetHandle ()
{
	if (_phConstantOptionletVolatility == NULL)
	{
		_phConstantOptionletVolatility = new Handle<QuantLib::ConstantOptionletVolatility> (*_ppConstantOptionletVolatility);
	}
	return *_phConstantOptionletVolatility;
}
#endif
bool Cephei::QL::Termstructures::Volatility::Optionlet::CConstantOptionletVolatility::HasNative () 
{
	return (_ppConstantOptionletVolatility != NULL);
}

DateTime Cephei::QL::Termstructures::Volatility::Optionlet::CConstantOptionletVolatility::MaxDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppConstantOptionletVolatility)->maxDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Optionlet::CConstantOptionletVolatility::MaxStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppConstantOptionletVolatility)->maxStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Optionlet::CConstantOptionletVolatility::MinStrike::get ()
{
    try
    {
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppConstantOptionletVolatility)->minStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Volatility::Optionlet::IConstantOptionletVolatility^ Cephei::QL::Termstructures::Volatility::Optionlet::CConstantOptionletVolatility_Factory::Create (DateTime referenceDate, Cephei::QL::Times::ICalendar^ cal, QL::Times::BusinessDayConventionEnum bdc, Double volatility, Cephei::QL::Times::IDayCounter^ dc)
{
    return REFNEW CConstantOptionletVolatility ( referenceDate,  cal,  bdc,  volatility,  dc);
}
Cephei::QL::Termstructures::Volatility::Optionlet::IConstantOptionletVolatility^ Cephei::QL::Termstructures::Volatility::Optionlet::CConstantOptionletVolatility_Factory::Create (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ cal, QL::Times::BusinessDayConventionEnum bdc, Double volatility, Cephei::QL::Times::IDayCounter^ dc)
{
    return REFNEW CConstantOptionletVolatility ( settlementDays,  cal,  bdc,  volatility,  dc);
}
Cephei::QL::Termstructures::Volatility::Optionlet::IConstantOptionletVolatility^ Cephei::QL::Termstructures::Volatility::Optionlet::CConstantOptionletVolatility_Factory::Create (DateTime referenceDate, Cephei::QL::Times::ICalendar^ cal, QL::Times::BusinessDayConventionEnum bdc, Cephei::QL::IQuote^ volatility, Cephei::QL::Times::IDayCounter^ dc)
{
    return REFNEW CConstantOptionletVolatility ( referenceDate,  cal,  bdc,  volatility,  dc);
}
Cephei::QL::Termstructures::Volatility::Optionlet::IConstantOptionletVolatility^ Cephei::QL::Termstructures::Volatility::Optionlet::CConstantOptionletVolatility_Factory::Create (UInt32 settlementDays, Cephei::QL::Times::ICalendar^ cal, QL::Times::BusinessDayConventionEnum bdc, Cephei::QL::IQuote^ volatility, Cephei::QL::Times::IDayCounter^ dc)
{
    return REFNEW CConstantOptionletVolatility ( settlementDays,  cal,  bdc,  volatility,  dc);
}
